﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SchedulerControl.Controls;


namespace SchedulerControl
{
    
    public enum ViewLayoutTypes
    {
        Day = 0,
        WorkWeek = 1,
        Week = 2,
        Month = 3
    }
    public partial class ComposedViewControl : UserControl
    {
        private ObservableCollection<AppointmentItemMetaData> _appointmentItems;

        int _daysCount = 0;
        private int _startDayWeek;
        private PersianCalendar persianCalendar;
        


        private ViewLayoutTypes _layoutType ;
        public ViewLayoutTypes LayoutType
        {
            get { return _layoutType; }
            set
            {
                _layoutType = value;
                ChangeView();
            }
        }

        public List<ScheduleDayViewControl> ScheduleDayViewControlItems { get; set; }

        private DateTime _startDate;

        public DateTime EndDate { get; set; }

        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {               
                _startDate = value;
                ChangeView();
            }
        }

        public ComposedViewControl()
        {
            InitializeComponent();
            persianCalendar = new PersianCalendar();
            ScheduleDayViewControlItems = new List<ScheduleDayViewControl>();
            _appointmentItems = new ObservableCollection<AppointmentItemMetaData>();
            persianDatePickerControl.OnCalendarItemClicked += new PersianCalendarControl.Controls.PersianDatePickerControl.CalendarItemClickedEvent(persianDatePickerControl_OnCalendarItemClicked);
            StartDate = DateTime.Now;
            LayoutType = ViewLayoutTypes.Day;
            ExecuteZoom();
        }

        void persianDatePickerControl_OnCalendarItemClicked(object sender, PersianCalendarControl.CalendarEventArgs e)
        {
            this.StartDate = e.SelectedDateTime; 
        }


        public void ChangeView()
        {
            
            int _daysCount = 0;
            DateTime tmpEndDate;
            switch (_layoutType)
            {
                case ViewLayoutTypes.Day:
                    _daysCount = 1;
					//_startDate = DateTime.Now; 
                    tbTimeSpanTitle.Text = persianCalendar.WeekDayNames[((int)persianCalendar.GetDayOfWeek(StartDate))] + " " + persianCalendar.GetDayOfMonth(StartDate) + " " + persianCalendar.MonthNames[persianCalendar.GetMonth(StartDate)] + " " + persianCalendar.GetYear(StartDate);
                    DayViewCheckbox.IsChecked = true;
                    break;
                case ViewLayoutTypes.WorkWeek:					
                	_startDate = persianCalendar.StartOfWeek(_startDate, DayOfWeek.Sunday);
                    tbTimeSpanTitle.Text = persianCalendar.WeekDayNames[((int)persianCalendar.GetDayOfWeek(StartDate))] + " " + persianCalendar.GetDayOfMonth(StartDate) + " " + persianCalendar.MonthNames[persianCalendar.GetMonth(StartDate)] + " " + persianCalendar.GetYear(StartDate);
                    tmpEndDate = StartDate.AddDays(4);
                    tbTimeSpanTitle.Text += " تا ";
                    tbTimeSpanTitle.Text += persianCalendar.WeekDayNames[((int)persianCalendar.GetDayOfWeek(tmpEndDate))] + " " + persianCalendar.GetDayOfMonth(tmpEndDate) + " " + persianCalendar.MonthNames[persianCalendar.GetMonth(tmpEndDate)] + " " + persianCalendar.GetYear(tmpEndDate);
                    _daysCount = 5;
                    WorkWeekViewCheckbox.IsChecked = true;
                    break;
                case ViewLayoutTypes.Week:					
                	_startDate = persianCalendar.StartOfWeek(_startDate, DayOfWeek.Sunday);
                    tbTimeSpanTitle.Text = persianCalendar.WeekDayNames[((int)persianCalendar.GetDayOfWeek(StartDate))] + " " + persianCalendar.GetDayOfMonth(StartDate) + " " + persianCalendar.MonthNames[persianCalendar.GetMonth(StartDate)] + " " + persianCalendar.GetYear(StartDate);
                    tmpEndDate = StartDate.AddDays(6);
                    tbTimeSpanTitle.Text += " تا ";
                    tbTimeSpanTitle.Text += persianCalendar.WeekDayNames[((int)persianCalendar.GetDayOfWeek(tmpEndDate))] + " " + persianCalendar.GetDayOfMonth(tmpEndDate) + " " + persianCalendar.MonthNames[persianCalendar.GetMonth(tmpEndDate)] + " " + persianCalendar.GetYear(tmpEndDate);
                    _daysCount = 7;
                    WeekViewCheckbox.IsChecked = true;
                    break;
                case ViewLayoutTypes.Month:
                    _daysCount = 35;
                    MonthViewCheckbox.IsChecked = true;
                    break;
            }
            
            

            if (_layoutType != ViewLayoutTypes.Month)
            {
                ScheduleDayViewControlItems.Clear();
				headersGrid.ColumnDefinitions.Clear();  
                daysLayoutGrid.ColumnDefinitions.Clear();
                DateTime tmpStartDate = this.StartDate; 
                for (int i = 0; i < _daysCount; i++)
                {
                    ColumnDefinition columnDefinition = new ColumnDefinition();
                    headersGrid.ColumnDefinitions.Add(columnDefinition);
                    ScheduleDayViewHeaderControl scheduleDayViewHeaderControl= new ScheduleDayViewHeaderControl();
                    scheduleDayViewHeaderControl.MouseLeftButtonDown += new MouseButtonEventHandler(scheduleDayViewHeaderControl_MouseLeftButtonDown);
                    scheduleDayViewHeaderControl.CurrentViewDate = tmpStartDate;
                    Grid.SetColumn(scheduleDayViewHeaderControl, i);
                    headersGrid.Children.Add(scheduleDayViewHeaderControl);

                    columnDefinition = new ColumnDefinition();
                    daysLayoutGrid.ColumnDefinitions.Add(columnDefinition);
                    ScheduleDayViewControl dayViewControl = new ScheduleDayViewControl();
                    dayViewControl.CurrentViewDate = tmpStartDate;
                    Grid.SetColumn(dayViewControl, i);
                    daysLayoutGrid.Children.Add(dayViewControl);
                    ScheduleDayViewControlItems.Add(dayViewControl);
                    tmpStartDate = tmpStartDate.AddDays(1);
                }
            }
        }

        void scheduleDayViewHeaderControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ScheduleDayViewHeaderControl scheduleDayViewHeaderControl = (ScheduleDayViewHeaderControl) sender;
            _startDate = scheduleDayViewHeaderControl.CurrentViewDate;
            LayoutType = ViewLayoutTypes.Day;
        }


        public void IncrementView()
        {
            switch (_layoutType)
            {
                case ViewLayoutTypes.Day:
                    StartDate = StartDate.AddDays(1);
                    break;
                case ViewLayoutTypes.WorkWeek:
                    StartDate = StartDate.AddDays(7);
                    break;
                case ViewLayoutTypes.Week:
                    StartDate = StartDate.AddDays(7);
                    break;
                case ViewLayoutTypes.Month:
                    StartDate = StartDate.AddDays(31);
                    break;
            }
        }

        public void DecrementView()
        {
            switch (_layoutType)
            {
                case ViewLayoutTypes.Day:
                    StartDate = StartDate.AddDays(-1);
                    break;
                case ViewLayoutTypes.WorkWeek:
                    StartDate = StartDate.AddDays(-7);
                    break;
                case ViewLayoutTypes.Week:
                    StartDate = StartDate.AddDays(-7);
                    break;
                case ViewLayoutTypes.Month:
                    StartDate = StartDate.AddDays(-31);
                    break;
            }
        }

        private decimal scrollableHeight;
        protected override Size MeasureOverride(Size availableSize)
        {
            base.MeasureOverride(availableSize);
            scrollableHeight = (decimal)(availableSize.Height - 110);
            
           if(zoomSlider.Value == currentZoomRatio)
            {
                if (currentZoomRatio <= 1)
                {
                    currentZoomRatio = 1;
                }
                ExecuteZoom();
            }
            else
            {
                zoomSlider.Value = currentZoomRatio;
            }

            return availableSize;
        }


        private void btnNavigateBack_Click(object sender, RoutedEventArgs e)
        {
            DecrementView(); 
        }

        private void btnNavigateForth_Click(object sender, RoutedEventArgs e)
        {
            IncrementView();
        }


        private double currentZoomRatio = 1;
        
        private void IncZoomButton_Click(object sender, RoutedEventArgs e)
        {
            if ((resizableGrid.Height * 2) <= ((double)scrollableHeight*16))
            {
                currentZoomRatio *= 2;
                if (currentZoomRatio == 1) currentZoomRatio = 0;
                zoomSlider.Value = currentZoomRatio;
                
            }
        }

        private void DecZoomButton_Click(object sender, RoutedEventArgs e)
        {
            if ((resizableGrid.Height / 2) >= (double)scrollableHeight)
            {
                currentZoomRatio /= 2;
                if (currentZoomRatio == 1) currentZoomRatio = 0;
                zoomSlider.Value = currentZoomRatio;
            }
            
        }

        private void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            currentZoomRatio = e.NewValue;
            if (currentZoomRatio <= 1)
            {
                currentZoomRatio = 1;
            }
            ExecuteZoom();
        }

        private void ExecuteZoom()
        {
            resizableGrid.Height = ((double)scrollableHeight) * currentZoomRatio;
        }

        private ChangeScheduleViewControl currentChangeScheduleViewControl;
        private void ChangeScheduleViewControl_Checked(object sender, RoutedEventArgs e)
        {
            currentChangeScheduleViewControl = (ChangeScheduleViewControl)sender;
            if (currentChangeScheduleViewControl != DayViewCheckbox) DayViewCheckbox.IsChecked = false;
            if (currentChangeScheduleViewControl != WorkWeekViewCheckbox) WorkWeekViewCheckbox.IsChecked = false;
            if (currentChangeScheduleViewControl != WeekViewCheckbox) WeekViewCheckbox.IsChecked = false;
            if (currentChangeScheduleViewControl != MonthViewCheckbox) MonthViewCheckbox.IsChecked = false;
            this.LayoutType = currentChangeScheduleViewControl.LayoutType;
        }

        private void ChangeScheduleViewControl_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {

            scrollableHeight = (decimal) e.NewSize.Height;
            
            
            if (zoomSlider.Value == currentZoomRatio)
            {
                if (currentZoomRatio <= 1)
                {
                    currentZoomRatio = 1;
                }
                ExecuteZoom();
            }else
            {
                zoomSlider.Value = currentZoomRatio;
            }
        }

    }
}
